package top;

/**
 * @author chenyw
 * @date 2022/7/6 18:57
 * @description
 */
public class Top38countAndSay {
    public String countAndSay(int n) {
        String str = "1";
        for (int i = 2;i <= n; i++) {
            StringBuffer sb = new StringBuffer();
            int start = 0;
            int pos = 0;
            //因为无法处理边界情况，所以不用str.charAt(i-1)这种写法
            while (pos < str.length()) {
                while (pos < str.length() && str.charAt(pos) == str.charAt(start)) {
                    pos++;
                }
                sb.append(Integer.toString(pos - start) + str.charAt(start));
                start = pos;
            }
            str = sb.toString();
        }
        return str;
    }
}
